<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class VerifyLogin extends CI_Controller {
	function __construct(){
		parent::__construct();
		$this->load->model('jugador', '', TRUE);
		$this->load->helper(array('form'));
		$this->load->library('form_validation');
	}
	function index(){
		$this->form_validation->set_rules('username', 'Usuario', 'trim|required|xss_clean');
		$this->form_validation->set_rules('password', 'Contraseña', 'trim|required|xss_clean|callback_check_database');
		
		$this->form_validation->set_error_delimiters('<div class="help-block">', '</div>');
		if($this->form_validation->run() == FALSE){
			$this->load->view('login_view');
		}else{
			redirect('home','refresh');
		}
	}
	
	function check_database($password){
		return $this->logear($this->input->post('username'), $password);
	}
	
	function logear($username, $password){
	
		$result = $this->jugador->login($username, $password);
		if($result){
			$this->session->set_userdata('logged_in', array('id' => $result->id, 'username' => $result->username ));
			return TRUE;
		}else{
			$this->form_validation->set_message('check_database', 'Usuario o contraseña invalido');
			return FALSE;
		}
	}
}
